package com.jl15988.simpletools.appcore.strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 存储策略工厂
 *
 * @author jl15988
 */
@Component
public class StorageStrategyFactory {

    @Value("${file.storage.type}")
    private String storageType;

    private final Map<String, StorageStrategy> strategyMap;

    @Autowired
    public StorageStrategyFactory(Map<String, StorageStrategy> strategyMap) {
        this.strategyMap = strategyMap;
    }

    /**
     * 获取存储策略
     *
     * @return 存储策略
     */
    public StorageStrategy getStrategy() {
        StorageStrategy strategy = strategyMap.get(storageType + "StorageStrategy");
        if (strategy == null) {
            // 默认使用本地存储
            strategy = strategyMap.get("localStorageStrategy");
        }
        return strategy;
    }

    /**
     * 获取指定类型的存储策略
     *
     * @param type 存储类型
     * @return 存储策略
     */
    public StorageStrategy getStrategy(String type) {
        StorageStrategy strategy = strategyMap.get(type + "StorageStrategy");
        if (strategy == null) {
            // 默认使用本地存储
            strategy = strategyMap.get("localStorageStrategy");
        }
        return strategy;
    }
} 